package net.cyue.web.easyquery.springboot;

import net.cyue.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConfigurationImportBeanRegistrar implements ImportBeanDefinitionRegistrar {

    private final Logger logger = LoggerFactory.getLogger(this.toString());

    public void registerBeanDefinitions(
        AnnotationMetadata importingClassMetadata,
        @NonNull BeanDefinitionRegistry registry
    ) {
        // 获取@EnableEasyQueryApplication注解的属性
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
            importingClassMetadata.getAnnotationAttributes(EnableEasyQueryApplication.class.getName())
        );

        if (attributes == null) {
            return;
        }
        // 解析配置路径
        String[] tomlFilePathArray = attributes.getStringArray("tomlFiles");
        String[] jsonFilePathArray = attributes.getStringArray("jsonFiles");
        String[] propertiesPathArray = attributes.getStringArray("properties");
        this.logger.info("解析到 EasyQueryApplication TOML 配置路径: {}", Arrays.toString(tomlFilePathArray));
        this.logger.info("解析到 EasyQueryApplication JSON 配置路径: {}", Arrays.toString(jsonFilePathArray));
        this.logger.info("解析到 EasyQueryApplication Properties 配置路径: {}", Arrays.toString(propertiesPathArray));

        List<String> tomlFilePathList = new ArrayList<>();
        List<String> jsonFilePathList = new ArrayList<>();
        for (String tomlFilePath : tomlFilePathArray) {
            if (StringUtil.isBlank(tomlFilePath)) {
                this.logger.error("EasyQueryApplication TOML 配置路径不能为空");
                continue;
            }
            if (!tomlFilePath.toLowerCase().endsWith(".toml")) {
                this.logger.error("EasyQueryApplication TOML 配置路径必须以 .toml 结尾");
                continue;
            }
            tomlFilePathList.add(tomlFilePath);
        }
        for (String jsonFilePath : jsonFilePathArray) {
            if (StringUtil.isBlank(jsonFilePath)) {
                this.logger.error("EasyQueryApplication JSON 配置路径不能为空");
                continue;
            }
            if (!jsonFilePath.toLowerCase().endsWith(".json")) {
                this.logger.error("EasyQueryApplication JSON 配置路径必须以 .json 结尾");
                continue;
            }
            jsonFilePathList.add(jsonFilePath);
        }

        // 注册EasyQueryProperties Bean
        BeanDefinition definition = BeanDefinitionBuilder
            .genericBeanDefinition(EasyQueryApplicationConfigurationContainer.class)
            .addConstructorArgValue(tomlFilePathList.toArray())
            .addConstructorArgValue(jsonFilePathList.toArray())
            .addConstructorArgValue(propertiesPathArray)
            .getBeanDefinition();

        registry.registerBeanDefinition("easyQueryApplicationPropertiesContainer", definition);
    }
}
